package com.ljx.offer;

import org.junit.Test;

/**
 * @author 李捷禧
 * Date: 2023/3/31
 * ClassName: C_49
 */

public class C_49 {

    /**
     * 丑数
     * 一个数的因子是2,3,5的组合就是丑数
     * @param n
     * @return
     */
    public int nthUglyNumber(int n) {
       int a = 0,b = 0 , c = 0;
       int[] res = new int[n];
       res[0] = 1;
        for (int i = 1; i < n; i++) {
            int n1 = res[a] * 2;
            int n2 = res[b] * 3;
            int n3 = res[c] * 5;
            //每次遍历就取三个数中最小的数
            res[i] = Math.min(Math.min(n1,n2),n3);
            if(n1 == res[i]) a++;
            if(n2 == res[i]) b++;
            if(n3 == res[i]) c++;
        }
        return res[n-1];
    }

    @Test
    public void test(){
        System.out.println(nthUglyNumber(12));
    }

}
